package com.lqd.mall.system.service.impl;

import com.lqd.mall.basis.exception.ExceptionCast;
import com.lqd.mall.interaction.system.SysUserEntity;
import com.lqd.mall.interaction.system.vo.LoginUserVo;
import com.lqd.mall.security.utils.SecurityUtils;
import com.lqd.mall.system.service.ISysMenuService;
import com.lqd.mall.system.service.ISysPostService;
import com.lqd.mall.system.service.ISysRoleService;
import com.lqd.mall.system.service.ISysUserLoginService;
import com.lqd.mall.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

/**
 * @author 零起点
 * @create 2022-02-20 0:34
 */
@Service
public class SysUserLoginServiceImpl implements ISysUserLoginService {

    @Autowired
    private ISysUserService sysUserService;

    //roleService
    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private ISysMenuService sysMenuService;
    @Autowired
    private ISysPostService sysPostService;


    @Override
    public LoginUserVo getUserInfo(String username) {
        LoginUserVo userVo = new LoginUserVo();
        List<SysUserEntity> userEntities = sysUserService.findSysUserEntitieList(new SysUserEntity().setUserName(username));
        if (userEntities.size() > 0) {
            userVo.setSysUser(userEntities.get(0));
        }
        Set<String> roles = sysRoleService.selectRolePermissionByUserId(userVo.getSysUser().getUserId());
        userVo.setRoles(roles);
        List<String> menuPermission = sysMenuService.getMenuPermission(userVo.getSysUser().getUserId());
        userVo.setPermissions(new HashSet<>(menuPermission));
        return userVo;
    }

    @Override
    public Map<String, Object> getInfo() {

        Long userId = SecurityUtils.getUserId();
        Set<String> roles = sysRoleService.selectRolePermissionByUserId(userId);
        List<String> permissions = sysMenuService.getMenuPermission(userId);
        Map<String,Object> data = new HashMap<>();

        data.put("user", sysUserService.selectUserById(userId));
        data.put("roles", roles);
        data.put("permissions", permissions);
        return data;

    }

    @Override
    public Map<String, Object> profile() {
        Long userId = SecurityUtils.getUserId();
        String roleGroup = sysRoleService.checkRoleNameUniqueByUserId(userId);
        String postGroup = sysPostService.selectUserPostGroup(userId);
        SysUserEntity user = sysUserService.selectUserById(userId) ;
        HashMap<String, Object> map = new HashMap<>();
        map.put("data",user);
        map.put("roleGroup",roleGroup);
        map.put("postGroup",postGroup);
        return map;
    }

    @Override
    public Integer updatePwd(String oldPassword, String newPassword) {
        Long userId = SecurityUtils.getUserId();
        SysUserEntity userEntity = sysUserService.selectUserById(userId);

        if (!SecurityUtils.matchesPassword(oldPassword,userEntity.getPassword() )) {
            ExceptionCast.cast("原始密码错误");
        }
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        SysUserEntity newUser  = new SysUserEntity();
        String encode = passwordEncoder.encode(newPassword);
        userEntity.setPassword(encode);
        newUser.setUserId(userId);
        newUser.setPassword(encode);
        Optional<Integer> integer = sysUserService.updateEntity(newUser, "user_id");
        return integer.get();
    }
}
